package com.java.base.exercise.designmode.responsibilitychain;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author RenPu
 * @Date 2021/6/27 9:28
 * @Version 1.0
 * @Description: 设计模式--责任链模式--简单调用v1.0
 **/
public class ResponsibilityChain {

    public static void main(String[] args) {

        //模仿请求进来访问输入的内容，有一些敏感词进行过滤
        Message message = new Message();
        message.setMsg("大家好:),<script>,欢迎访问 www.baidu.com, 大家都是007");


        /**---------------1.0过滤----------------------*/
//        String msg = message.getMsg();
//        msg=msg.replace('<','[');
//        msg=msg.replace('>',']');
//        message.setMsg(msg);
//        msg=msg.replaceAll("007","955");
//        message.setMsg(msg);

//        SymbolFilter symbolFilter = new SymbolFilter();
//        symbolFilter.doFilter(message);
//        NumberFilter numberFilter = new NumberFilter();
//        numberFilter.doFilter(message);


        /**---------------2.0过滤----------------------*/
//        List<FilterDefinnte> definnteList = new ArrayList<>();
//        definnteList.add(new SymbolFilter());
//        definnteList.add(new NumberFilter());
//
//        for (FilterDefinnte definnte: definnteList) {
//            definnte.doFilter(message);
//        }
//


        /**---------------3.0过滤----------------------*/
        ChainFilter chainFilter = new ChainFilter();
        chainFilter.add(new SymbolFilter()).add(new NumberFilter());

        ChainFilter chainFilter1 = new ChainFilter();
        chainFilter1.add(new UrlFilter()).add(new FaceFilter());

        chainFilter.add(chainFilter1).doFilter(message);
        System.out.println(message);

    }


}

class Message {

    String name;
    String msg;


    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "Message{" +
                " msg='" + msg + '\'' +
                '}';
    }
}


//创建接口，使用接口实现多态的展示
interface FilterDefinnte {
    boolean doFilter(Message message);
}


//过滤符号的实现
class SymbolFilter implements FilterDefinnte {
    @Override
    public boolean doFilter(Message message) {
        String msg = message.getMsg();
        msg = msg.replace('<', '[');
        msg = msg.replace('>', ']');
        message.setMsg(msg);
        return true;
    }
}


//过滤数字
class NumberFilter implements FilterDefinnte {
    @Override
    public boolean doFilter(Message message) {
        String msg = message.getMsg();
        msg = msg.replaceAll("007", "955");
        message.setMsg(msg);
        return false;
    }
}


class ChainFilter implements FilterDefinnte {
    List<FilterDefinnte> definnteList = new ArrayList<>();

    /**
     * 添加方法
     */
    public ChainFilter add(FilterDefinnte filterDefinnte) {
        definnteList.add(filterDefinnte);
        return this;
    }


    /**
     * 执行方法
     */
    @Override
    public boolean doFilter(Message message) {
        for (FilterDefinnte definnte : definnteList) {
            if (!definnte.doFilter(message)) return false;
        }
        return true;
    }


}

class FaceFilter implements FilterDefinnte {
    @Override
    public boolean doFilter(Message message) {
        String msg = message.getMsg();
        msg = msg.replace(":)", "O(∩_∩)O哈哈~");
        message.setMsg(msg);
        return true;
    }

}

class UrlFilter implements FilterDefinnte {

    @Override
    public boolean doFilter(Message message) {
        String msg = message.getMsg();
        msg = msg.replace(" www.baidu.com", " https://www.baidu.com");
        message.setMsg(msg);
        return true;
    }

}